﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Common;
using GroupChat2013.Interface;
using GroupChat2013.Logic;
using GroupChat2013.Logic.Interface;

namespace GroupChat2013.Main.Components
{
    /// <summary>
    /// 私聊接受处理
    /// </summary>
    public class UserChatReceiveProcess : IReceiveProcess
    {
        private FrmChatBox frmChatBox;

        /// <summary>
        /// 消息处理标志
        /// </summary>
        private string key;

        private string senderId = "senderId";

        private string name = "name";

        private string unread = "unread";

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserChatReceiveProcess(FrmChatBox frmChatBox)
        {
            this.frmChatBox = frmChatBox;
            key = ConstClass.GETLATESTPERSONCHAT;
        }

        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool Process(Entity.Protocol.TransferMsgInfo message)
        {
            if (!message.action.Equals(key))
                return false;

            if (message.data.ContainsKey(senderId))
            {
                string id = message.data[senderId];
                IChat chat = EngineContext.Current.ChatManager[id];
                if (chat != null)
                {
                    chat.UpdateChat();
                }
                else
                {
                    frmChatBox.AddItem(id, message.data[name], message.data[unread], Entity.ChatType.User);
                }
            }

            return true;
        }
    }
}
